﻿using System;
using System.Collections.Generic;
using Aierp.Entities;
using Aierp.Repository.Models;
using Aierp.SystemEntities.System;
using Aierp.Utility.Data;
using Chloe;

namespace Aierp.Repository
{
    public class UserRolesRepository : DbInstance, IRepository<Sys_UserRoles>
    {
        public int Add(Sys_UserRoles item)
        {
            //先判断是否是超级管理员admin，如果是超级管理员，那么角色默认是管理员角色,管理员不允许修改角色
            var userName = this.FindBy<Sys_Users>(c => c.KeyId == item.UserID).UserName;

            if (userName == "admin") return -9;
            
            //先检查是否存在,存在则进行更新，不存在则添加
            //var entity = this.FindBy<Sys_UserRoles>(c => c.RoleID == item.RoleID && c.UserID== item.UserID);//对应一个人多个角色
            var entity = this.FindBy<Sys_UserRoles>(c => c.UserID == item.UserID);//对应一个人1个角色
            if (entity == null)
            {
               var result = Db.Insert<Sys_UserRoles>(item);
               return result.KeyId;
            }

            //更新
            Db.TrackEntity(item);//在上下文中跟踪实体
            entity.RoleID = item.RoleID;
            return Db.Update(entity);//这时只会更新被修改的字段
        }

        public Sys_UserRoles Find(string key)
        {
            //return this.FindBy<Sys_UserRoles>(c => c.RoleName == key);
            return null;
        }

        public List<Sys_UserRoles> FindAll()
        {
            throw new NotImplementedException();
        }

        public Sys_UserRoles FindById(int id)
        {
            //先判断是否是超级管理员admin，如果是超级管理员admin，那么角色默认是管理员角色
            var userName = this.FindBy<Sys_Users>(c => c.KeyId == id).UserName;
            if (userName == "admin")
            {
                var role = this.FindBy<Sys_Roles>(c => c.RoleName == "管理员");
                if (role == null) return null;

                var view = new UserRoleView
                {
                    KeyId = 0,
                    RoleID = role.KeyId,
                    RoleName = role.RoleName,
                    UserID = id,
                    UserName = userName
                };
                return view;
            }
            //判断是否是管理员结束

            //判断该用户是否已经有角色存在
            if (FindBy<Sys_UserRoles>(c => c.UserID == id) == null) return null;

            Sys_UserRoles model = null;
            IEnumerable<UserRoleView> userRoleView = Db.SqlQuery<UserRoleView>(
                "select u.KeyId,u.UserName,u.TrueName,rs.RoleID,s.RoleName from [Sys_Users] u left join Sys_UserRoles rs ON rs.UserID = u.KeyId left join Sys_Roles s ON rs.RoleID = s.KeyId where u.KeyId=@id",
                DbParam.Create("@id", id));
            foreach (var s in userRoleView)
            {
                model = s;
            }
            return model;
        }

        public string GetList(int limit, int offset, string searchTxt, string sortOrder)
        {
            throw new NotImplementedException();
        }

        public int Remove(int key)
        {
            throw new NotImplementedException();
        }

        public int Update(Sys_UserRoles item)
        {
            return 0;
        }
    }
}
